//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <List.h>
#include "TMainForm.h"
#include "TranslateUZ.h"
#include "WKls.h"
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
const AnsiString inputFileName = "objects_name.cfg";
AnsiString   EtalonWord = "";
TStringList* StrListPtr = NULL;

//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
	StrListPtr = new TStringList();
	try
	{
		StrListPtr->LoadFromFile(inputFileName);
		EtalonWord = StrListPtr->Text;
		EtalonWord = EtalonWord.UpperCase( );

	}
	catch(...)
	{
		delete StrListPtr;
		StrListPtr = NULL;
	}

}
__fastcall TMainForm::~TMainForm()
{
	if( NULL != DataBase )
	{
		delete DataBase;
	}
}

bool Compare(  AnsiString Word )
{
	Word = Word.UpperCase( );

	if ( EtalonWord.Pos("%%"+Word+"%%") > 0)
	{
	   return true;
	}
	return false;
}
void pchFind_word ( AnsiString Text, int posX,int posY )
{
	AnsiString Word   = "";
	char*      chText = NULL;
	chText = Text.c_str();

	MainForm->Code->SelStart = 0;
	MainForm->Code->SelLength = MainForm->Code->Text.Length();
	MainForm->Code->SelAttributes->Color = clBlack;

	for ( unsigned int i = 0; i < strlen( chText ); i++ )
	{
		unsigned int j = i;
		while( isalnum( chText[j] ) )
		{
			j++;
		}

		if( i!=j )
		{
			if (Compare( Text.SubString(i+1,j-i) ))
            {
				MainForm->Code->SelStart = i;
				MainForm->Code->SelLength = j-i;
				MainForm->Code->SelAttributes->Color = clBlue;
            }
		}

        i = j;
    }

}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Open1Click(TObject *Sender)
{
	AnsiString fileName = "";
	int err = 0;
	GBASE_OBJECT Object;
	GBASE_QUERY Query;
	BASE_INT *pSem = new BASE_INT[1000];
	BASE_INT Code[9]={0,};

	if( this->OpenDialog1->Execute( NULL ) )
	{
		TreeView1->Items->Clear();
	  //	this->OpenDialog1->FileName = "C:\\program\\SEAMAP2.ZNL";
		DataBase = new TDataBase( &err,this->OpenDialog1->FileName.c_str(),"R","ABD","");
		fileName = ExtractFileName( this->OpenDialog1->FileName );
		MainNode = TreeView1->Items->Add( NULL, fileName );
	}
	if (err == 0)
	{
		DataBase->BaseInitObject( &Object,pSem,1000*2,NULL,1,NULL,1,NULL,0,NULL,
								  0,NULL,0,NULL,0 );
		DataBase->BaseInitQuery( &Query,32767,1 );

		err=DataBase->BaseReadObject( &Query,
									  NULL,
									  OPEN_TREE,
									  OBJ_FULL,
									  &Object,
									  Code,
									  0 );


		while ( err == BERR_OK )
		{
			DataBase->BaseCloseobject( &Object );
			err = DataBase->BaseReadObject( &Query,NULL,READ_NEXT,OBJ_FULL ,
			&Object,Code,0 );
			TCode tCode( Object.Code );
 			CreateTree( &tCode );
		}
	}
}

TTreeNode* __fastcall TMainForm::FindChild( TTreeNode *pNode, int nNode )
{
	TTreeNode *pTmpNode = pNode->getFirstChild();

	if( pTmpNode == NULL )
	{
		return NULL;
	}

	do{

		if( pTmpNode->Text == (AnsiString)nNode )
		{
			return pTmpNode;
		}

		pTmpNode = pNode->GetNextChild( pTmpNode );

	}
	while( pTmpNode != NULL  );

	return NULL;


}

void __fastcall TMainForm::CreateTree(TCode *tCode)
{
	TTreeNode *pNode = this->MainNode;

	for(int i = 0;i<SIZE_CODE;i++)
	{
		TTreeNode *pTmpNode = FindChild( pNode, tCode->GetData(i) );

		if(0 == tCode->GetData(i) )
		{
			break;
		}

		if( pTmpNode != pNode && pTmpNode != NULL )
		{
			pNode = pTmpNode;
			continue;
		}

		pNode = TreeView1->Items->AddChild(pNode,(AnsiString)tCode->GetData(i) );

	}

}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TreeView1DblClick(TObject *Sender)
{
    TTreeNode *pSelectNode = TreeView1->Selected;
    short sCode[SIZE_CODE]={0};
	GBASE_OBJECT Object;
	GBASE_QUERY Query;
	BASE_INT *pSem = new BASE_INT[1000];

    int i = 0;

	while(NULL!=pSelectNode->Parent)
	{
        sCode[i+1]=pSelectNode->Text.ToInt();
        i++;
        pSelectNode=pSelectNode->Parent;
    }
    sCode[0] = i;
    i--;
    for(int j=0;i/2>j>0;j++)
    {
		short tmp = sCode[j+1];
        sCode[j+1]=sCode[i-j+1];
        sCode[i-j+1]=tmp;
    }


		DataBase->BaseInitObject( &Object,pSem,1000*2,NULL,1,NULL,1,NULL,0,NULL,
								  0,NULL,0,NULL,0 );
		DataBase->BaseInitQuery( &Query,32767,1 );

		DataBase->BaseReadObject( &Query,
								  NULL,
								  OPEN_TREE,
								  OBJ_FULL,
								  &Object,
								  sCode,
								  0 );

        TranslateUZ TUZ( (TCode)sCode, uzOBJ, (UZ_Data *)Object.pMet );
        this->Code->Text =  TUZ.GetText();
	int        posX  = MainForm->Code->CaretPos.x;
	int        posY  = MainForm->Code->CaretPos.y;
	AnsiString Text  = MainForm->Code->Text;
	pchFind_word ( Text, posX, posY );
	Code->CaretPos = Point( posX,posY);



}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CodeKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
	int        posX  = MainForm->Code->CaretPos.x;
	int        posY  = MainForm->Code->CaretPos.y;
	AnsiString Text  = MainForm->Code->Text;
	pchFind_word ( Text, posX, posY );
	Code->CaretPos = Point( posX,posY);
}
//---------------------------------------------------------------------------

